package com.corechina.apex_osswords.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.corechina.apex_osswords.domain.dao.SysUserMapper;
import com.corechina.apex_osswords.domain.dao.SysUserTokenMapper;
import com.corechina.apex_osswords.domain.entity.SysUserToken;
import com.corechina.apex_osswords.domain.vo.SysUserForTokenVo;
import com.corechina.apex_osswords.service.IShiroService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author Styx
 */
@Service
public class ShiroServiceImpl implements IShiroService {
    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysUserTokenMapper sysUserTokenMapper;


    @Override
    public Set<String> getUserPermissions(Integer userId) {
        List<String> permsList;
        permsList = sysUserMapper.getAllPerms(userId);
        //用户权限列表
        Set<String> permsSet = new HashSet<>();
        for (String perms : permsList) {
            if (StringUtils.isEmpty(perms)) {
                continue;
            }
            permsSet.addAll(Arrays.asList(perms.trim().split(",")));
        }
        return permsSet;
    }

    @Override
    public SysUserToken getByToken(String token) {
        QueryWrapper<SysUserToken> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("token", token);
        return sysUserTokenMapper.selectOne(queryWrapper);
    }

    @Override
    public SysUserForTokenVo getUser(Integer userId) {
        return sysUserMapper.getUserInfoForToken(userId);
    }

}
